﻿package com.crl.module.logging
{
	import com.crl.module.logging.events.LoggerEvent;
	import flash.events.EventDispatcher;
	
	internal class LogLogger extends EventDispatcher implements ILogger
	{
		private var _channel:String ;
		public function LogLogger( channel:String )
		{
			_channel = channel;
		}
		
		public function get channel():String
		{
			return _channel ;
		}
		
		
		public function debug( context:* , ...rest ):void
		{
			_log( LoggerLevel.DEBUG , context , rest ) ;
		}
		
		public function error( context:* , ...rest ):void
		{
			_log( LoggerLevel.ERROR , context , rest ) ;
		}
		
		
		public function fatal(context:*, ...rest):void
		{
			_log( LoggerLevel.FATAL , context , rest ) ;
		}
		
		public function info(context:*, ...rest):void
		{
			_log( LoggerLevel.INFO , context , rest ) ;
		}
		
		
		public function log( context:*, ...rest ):void
		{
			_log( LoggerLevel.ALL , context , rest ) ;
		}
		
		
		public function warn(context:*, ...rest):void
		{
			_log( LoggerLevel.WARN , context , rest ) ;
		}

		private function _log( level:LoggerLevel, context:*, options:Array ):void
		{
			if( hasEventListener( LoggerEvent.LOG ) )
			{
				if ( context is String )
				{
					var len:int = options.length ;
					for( var i:int ; i<len ; i++ )
					{
						context = (context as String).replace( new RegExp( "\\{" + i + "\\}" , "g" ) , options[i]);
					}
				}
				dispatchEvent( new LoggerEvent( context, level ) ) ;
			}
		}
	}
}